<?php

namespace App\Http\Middleware;

use App\Services\ISessionService;
use Closure;
use Log;
use View;

/**
 * 自动重定向记录
 * @package App\Http\Middleware
 */
class RedirectUrl
{

    private $sessionService;

    public function __construct(ISessionService $sessionService)
    {
        $this->sessionService = $sessionService;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$request->ajax()){
            if (($referer = $request->header('referer')) && ($referer != url()->full())){
                $this->sessionService->setRefererUrl($referer);
            }
            if ($request->has('redirectUrl')){
                $this->sessionService->setRedirectUrl($request->input('redirectUrl'));
            }
            View::share('_refer',$this->sessionService->getRefererUrl());
        }

        return $next($request);
    }
}
